package pruebas;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import junit.framework.TestCase;
import negModel.AlgoPesos;
import negModel.Transmision;
import negModelExcep.*;

public class TransmisionTest extends TestCase {

	Transmision trans,trans2;
	Element e;
	
	protected void setUp() throws Exception {
		super.setUp();
		trans = new Transmision(100,50,50,new String("trans"),new AlgoPesos(100),false,50,5,true);

		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
	    DocumentBuilder loader = factory.newDocumentBuilder();
	    Document doc = loader.newDocument();	
	    
	    e = doc.createElement("parte");
		e.setAttribute("Clase","transmision");
		
		e.setAttribute("maxVU", ""+ trans.getMaxVU());
		e.setAttribute("vidaUtil", ""+ trans.getVidaUtil());
		e.setAttribute("peso", ""+ trans.getPeso());
		e.setAttribute("aportevmax", ""+ trans.aportevmax());
		e.setAttribute("detalle", trans.getDetalle());
		e.setAttribute("precio", ""+(trans.getPrecio()).getMonto());
		e.setAttribute("removible", ""+trans.esRemovible());
		e.setAttribute("performance", ""+trans.getPerformance());
		
		e.setAttribute("maxcambios", ""+trans.getMaxCambios());
		e.setAttribute("cambio", ""+trans.getCambio());
		e.setAttribute("manual", ""+trans.esManual());
		
	}

	public void testGastar() {
		try {
			trans.gastar();
		} catch (eParteNoFunciona e) {
			fail("se elevo la excepcion");
		}
		assertEquals(99,trans.getVidaUtil(),0.0);
	}

	public void testFuncionar() {
		try {
			assertEquals(0,trans.funcionar(),0.0);
		} catch (eParteNoFunciona e) {
			fail("se elevo la excepcion");
		}
	}

	public void testSubirCambio() {
		trans.subirCambio();
		assertEquals(1,trans.getCambio(),0.0);
		
	}

	public void testBajarCambio() {
		trans.bajarCambio();
		assertEquals(0,trans.getCambio(),0.0);
		
	}
	

	public void testEsManual() {
		assertEquals(true,trans.esManual());
	}
	
	public void testSetCambio(){
		trans.setCambio(100, 1000);
		assertEquals(1,trans.getCambio());
		trans.setCambio(300, 1000);
		assertEquals(2,trans.getCambio());
		trans.setCambio(500, 1000);
		assertEquals(3,trans.getCambio());
		trans.setCambio(700, 1000);
		assertEquals(4,trans.getCambio());
		trans.setCambio(800, 1000);
		assertEquals(5,trans.getCambio());
		trans.setCambio(1200, 1000);
		assertEquals(5,trans.getCambio());
	}
	
	public void testToXml () throws Exception {
		
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
	    DocumentBuilder loader = factory.newDocumentBuilder();
	    Document doc = loader.newDocument();
		
		e=trans.toXml(doc);
		
		assertEquals(trans.getMaxCambios(),Integer.parseInt(e.getAttribute("maxcambios")));
		assertEquals(trans.getCambio(),Integer.parseInt(e.getAttribute("cambio")));
		assertEquals(trans.esManual(),Boolean.parseBoolean(e.getAttribute("manual")));
		
		assertEquals(trans.getPerformance(),Integer.parseInt(e.getAttribute("performance")));
		assertEquals((trans.getPrecio()).getMonto(),Double.parseDouble(e.getAttribute("precio")),0.0);
		assertEquals(trans.aportevmax(),Float.parseFloat(e.getAttribute("aportevmax")),0.0);
		assertEquals(trans.getDetalle(),e.getAttribute("detalle"));
		assertEquals(trans.getPeso(),Double.parseDouble(e.getAttribute("peso")),0.0);
		assertEquals(trans.getVidaUtil(),Integer.parseInt(e.getAttribute("vidaUtil")));
		
	}
	
	public void testTransmision()throws Exception {
		
		trans2=new Transmision(e);
				
		assertEquals(trans2.getMaxCambios(),Integer.parseInt(e.getAttribute("maxcambios")));
		assertEquals(trans2.getCambio(),Integer.parseInt(e.getAttribute("cambio")));
		assertEquals(trans2.esManual(),Boolean.parseBoolean(e.getAttribute("manual")));
		
		assertEquals(trans2.getPerformance(),Integer.parseInt(e.getAttribute("performance")));
		assertEquals((trans2.getPrecio()).getMonto(),Double.parseDouble(e.getAttribute("precio")),0.0);
		assertEquals(trans2.aportevmax(),Float.parseFloat(e.getAttribute("aportevmax")),0.0);
		assertEquals(trans2.getDetalle(),e.getAttribute("detalle"));
		assertEquals(trans2.getPeso(),Double.parseDouble(e.getAttribute("peso")),0.0);
		assertEquals(trans2.getVidaUtil(),Integer.parseInt(e.getAttribute("vidaUtil")));
		
	}


}
